package com.huike.stu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huike.stu.dao.StudentDao;
import com.huike.stu.domain.Student;
import com.huike.utils.DBUtils;

public class StudentDaoImpl implements StudentDao {

	public int add(Student stu) {
		Connection conn = null;
		PreparedStatement pst = null;
		int count = 0;
		try {
			conn = DBUtils.getConn();
			String stuAddSQL = "INSERT INTO t_student(stu_no, stu_name, stu_sex, stu_age, stu_dept)"
					+ " VALUES(?, ?, ?, ?, ?)";
			pst = conn.prepareStatement(stuAddSQL);
			pst.setString(1, stu.getNo());
			pst.setString(2, stu.getName());
			pst.setString(3, stu.getSex());
			pst.setInt(4, stu.getAge());
			pst.setString(5, stu.getDept());
			count = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDB(conn, pst, null);
		}
		return count;
	}

	public int update(Student stu) {
		Connection conn = null;
		PreparedStatement pst = null;
		int counts = 0;
		try {
			conn = DBUtils.getConn();
			String stuUpdateSQL = "UPDATE t_student"
					+ " SET stu_name=?, stu_sex=?,stu_age=?,stu_dept=?"
					+ " WHERE stu_no=?";
			pst = conn.prepareStatement(stuUpdateSQL);
			pst.setString(1, stu.getName());
			pst.setString(2, stu.getSex());
			pst.setInt(3, stu.getAge());
			pst.setString(4, stu.getDept());
			pst.setString(5, stu.getNo());
			counts = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDB(conn, pst, null);
		}
		return counts;
	}

	public int disable(String no) {
		Connection conn = null;
		PreparedStatement pst = null;
		int counts = 0;
		try {
			conn = DBUtils.getConn();
			String stuDisableSQL = "UPDATE t_student SET stu_display=0 WHERE stu_no=?";
			pst = conn.prepareStatement(stuDisableSQL);
			pst.setString(1, no);
			counts = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDB(conn, pst, null);
		}
		return counts;
	}

	public Student find(String no) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Student stu = null;
		try {
			conn = DBUtils.getConn();
			String stuInfoSQl = "SELECT stu_no, stu_name, stu_sex, stu_age, stu_dept"
					+ " FROM t_student"
					+ " WHERE stu_no = ?";
			pst = conn.prepareStatement(stuInfoSQl);
			pst.setString(1, no);
			rs = pst.executeQuery();
			if (rs.next()) {
				stu = new Student();
				stu.setNo(rs.getString("stu_no"));
				stu.setName(rs.getString("stu_name"));
				stu.setSex(rs.getString("stu_sex"));
				stu.setAge(rs.getInt("stu_age"));
				stu.setDept(rs.getString("stu_dept"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDB(conn, pst, rs);
		}
		return stu;
	}

	public List<Student> findAll() {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Student> stus = null;
		try {
			conn = DBUtils.getConn();
			String stuListSQL = "SELECT stu_no, stu_name, stu_sex, stu_age, stu_dept"
					+ " FROM t_student"
					+ " WHERE stu_display = 1";
			pst = conn.prepareStatement(stuListSQL);
			rs = pst.executeQuery();
			stus = new ArrayList<Student>();
			while (rs.next()) {
				Student stu = new Student();
				stu.setNo(rs.getString("stu_no"));
				stu.setName(rs.getString("stu_name"));
				stu.setSex(rs.getString("stu_sex"));
				stu.setAge(rs.getInt("stu_age"));
				stu.setDept(rs.getString("stu_dept"));
				stus.add(stu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDB(conn, pst, rs);
		}
		return stus;
	}

}
